package com.kamistoat.ruoyi.ruoyimodulessystem.controller;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.kamistoat.ruoyi.ruoyiapisystem.domain.SysNotice;
import com.kamistoat.ruoyi.ruoyiapisystem.entity.SysNoticeEntity;
import com.kamistoat.ruoyi.ruoyicommoncore.web.controller.BaseController;
import com.kamistoat.ruoyi.ruoyicommoncore.web.domain.AjaxResult;
import com.kamistoat.ruoyi.ruoyicommoncore.web.page.TableDataInfo;
import com.kamistoat.ruoyi.ruoyicommonlog.annotation.Log;
import com.kamistoat.ruoyi.ruoyicommonlog.enums.BusinessType;
import com.kamistoat.ruoyi.ruoyicommonsecurity.annotation.RequiresPermissions;
import com.kamistoat.ruoyi.ruoyicommonsecurity.utils.SecurityUtils;
import com.kamistoat.ruoyi.ruoyimodulessystem.service.SysNoticeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 通知公告表
 *
 * @author kamistoat
 * @email linjianshanke@163.com
 * @date 2022-08-09 10:34:45
 */
@RestController
@RequestMapping("/notice")
public class SysNoticeController extends BaseController {
    @Autowired
    private SysNoticeService sysNoticeService;

    /**
     * 获取通知公告列表
     */
    @RequiresPermissions("system:notice:list")
    @GetMapping("/list")
    public TableDataInfo list(SysNotice notice) {
        startPage();
        List<SysNoticeEntity> sysNoticeEntityList = sysNoticeService.selectNoticeList(notice);
        return getDataTable(entity2Vo(sysNoticeEntityList));
    }

    /**
     * 根据通知公告编号获取详细信息
     */
    @RequiresPermissions("system:notice:query")
    @GetMapping(value = "/{noticeId}")
    public AjaxResult getInfo(@PathVariable("noticeId") Long noticeId) {
        return AjaxResult.success(entity2Vo(sysNoticeService.getById(noticeId)));
    }

    /**
     * 新增通知公告
     */
    @RequiresPermissions("system:notice:add")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysNotice notice) {
        notice.setCreateBy(SecurityUtils.getUserName());
        return toAjax(sysNoticeService.save(vo2Entity(notice)));
    }

    /**
     * 修改通知公告
     */
    @RequiresPermissions("system:notice:edit")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysNotice notice) {
        notice.setUpdateBy(SecurityUtils.getUserName());
        return toAjax(sysNoticeService.updateById(vo2Entity(notice)));
    }

    /**
     * 删除通知公告
     */
    @RequiresPermissions("system:notice:remove")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeIds}")
    public AjaxResult remove(@PathVariable Long[] noticeIds) {
        return toAjax(sysNoticeService.removeByIds(Arrays.asList(noticeIds)));
    }

    /**
     * Entity 和 Vo类的转换
     */
    private List<SysNotice> entity2Vo(List<SysNoticeEntity> sysNoticeEntityList) {
        return
                sysNoticeEntityList.stream().map(sysNoticeEntity -> {
                    SysNotice sysNotice = new SysNotice();
                    BeanUtils.copyProperties(sysNoticeEntity, sysNotice);
                    return sysNotice;
                }).collect(Collectors.toList());
    }

    private SysNotice entity2Vo(SysNoticeEntity sysNoticeEntity) {
        SysNotice sysNotice = new SysNotice();
        BeanUtils.copyProperties(sysNoticeEntity, sysNotice);
        return sysNotice;
    }

    private List<SysNoticeEntity> vo2Entity(List<SysNotice> sysNoticeList) {
        return
                sysNoticeList.stream().map(sysNotice -> {
                    SysNoticeEntity sysNoticeEntity = new SysNoticeEntity();
                    BeanUtils.copyProperties(sysNotice, sysNoticeEntity);
                    return sysNoticeEntity;
                }).collect(Collectors.toList());
    }

    private SysNoticeEntity vo2Entity(SysNotice sysNotice) {
        SysNoticeEntity sysNoticeEntity = new SysNoticeEntity();
        BeanUtils.copyProperties(sysNotice, sysNoticeEntity);
        return sysNoticeEntity;
    }

}
